<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-search"
          size="mini"
          @click="handleSearch"
          >搜索</el-button
        >
      </el-col>
      <!--      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['production:NoPlanAssign:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['production:NoPlanAssign:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['production:NoPlanAssign:remove']"
        >删除</el-button>
      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          :loading="exportLoading"
          @click="handleExport('#NoPlanAssign', '无单分配记录')"
          v-hasPermi="['production:NoPlanAssign:export']"
        >
          导出
        </el-button>
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      id="NoPlanAssign"
      @scrollBottom="load"
      highlight-current-row
      border
      stripe
      v-loading="loading"
      :data="NoPlanAssignList"
      @selection-change="handleSelectionChange"
      :header-cell-style="{ 'text-align': 'center' }"
    >
      <el-table-column label="#" width="40" align="center" type="index" />
      <el-table-column
        label="机台编号"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="noPlanJtbh"
      />
      <el-table-column
        label="制造单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="noPlanZzdh"
      />
      <el-table-column
        label="销售单号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="noPlanSodh"
      />
      <el-table-column
        label="模具编号"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="noPlanMjbh"
      />
      <el-table-column
        label="客户编码"
        :show-overflow-tooltip="true"
        width="120"
        align="center"
        prop="itmCustwldm"
      />
      <el-table-column
        label="品名规格"
        :show-overflow-tooltip="true"
        width="280"
        align="center"
        prop="itmPmgg"
      />
      <el-table-column
        label="计划数量"
        :show-overflow-tooltip="true"
        width="80"
        align="center"
        prop="noPlanScsl"
      />
      <el-table-column
        label="无单合模数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="noPlanHmsl"
      />
      <el-table-column
        label="无单良品数量"
        :show-overflow-tooltip="true"
        width="100"
        align="center"
        prop="noPlanLpsl"
      />
      <el-table-column
        label="分配时间"
        :show-overflow-tooltip="true"
        align="center"
        prop="noPlanTime"
        width="150"
      >
        <template #default="scope">
          <span>{{
            parseTime(scope.row.noPlanTime, "{y}-{m}-{d} {h}:{i}:{s}")
          }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="未分配前良品数量"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="noPlanBeforeLpsl"
      />
      <el-table-column
        label="分配后良品数量"
        :show-overflow-tooltip="true"
        width="130"
        align="center"
        prop="noPlanAfterLpsl"
      />
      <!--      <el-table-column label="操作" align="center" width="100" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" v-hasPermi="['production:NoPlanAssign:edit']">
            修改
          </el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)" v-hasPermi="['production:NoPlanAssign:remove']">
            删除
          </el-button>
        </template>
      </el-table-column>-->
    </el-table>

    <!-- 查询对话框   -->
    <el-dialog
      :title="title"
      v-model="openSearch"
      :close-on-click-modal="false"
      draggable
      width="400px"
      append-to-body
    >
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="68px"
      >
        <el-col :span="24">
          <el-form-item label="日期">
            <el-date-picker
              v-model="daterangeNoPlanTime"
              size="small"
              style="width: 240px"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="daterange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00', '23:59:59']"
              :clearable="false"
            ></el-date-picker>
          </el-form-item>
        </el-col>

        <el-col :span="24">
          <el-form-item label="机台编号" prop="noPlanJtbh">
            <el-input
              v-model="queryParams.noPlanJtbh"
              placeholder="请输入机台编号"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            style="margin-top: 25px; display: flex; justify-content: flex-end"
          >
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="handleQuery">确认</el-button>
          </el-form-item>
        </el-col>
      </el-form>
    </el-dialog>

    <!-- 添加或修改NoPlanAssign对话框 -->
    <el-dialog
      :title="title"
      v-model="open"
      :close-on-click-modal="false"
      draggable
      width="500px"
      append-to-body
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="制造单号" prop="noPlanZzdh">
          <el-input v-model="form.noPlanZzdh" placeholder="请输入制造单号" />
        </el-form-item>
        <el-form-item label="销售单号" prop="noPlanSodh">
          <el-input v-model="form.noPlanSodh" placeholder="请输入销售单号" />
        </el-form-item>
        <el-form-item label="物料代码" prop="noPlanWldm">
          <el-input v-model="form.noPlanWldm" placeholder="请输入物料代码" />
        </el-form-item>
        <el-form-item label="计划数量" prop="noPlanScsl">
          <el-input v-model="form.noPlanScsl" placeholder="请输入计划数量" />
        </el-form-item>
        <el-form-item label="机台编号" prop="noPlanJtbh">
          <el-input v-model="form.noPlanJtbh" placeholder="请输入机台编号" />
        </el-form-item>
        <el-form-item label="无单合模数量" prop="noPlanHmsl">
          <el-input
            v-model="form.noPlanHmsl"
            placeholder="请输入无单合模数量"
          />
        </el-form-item>
        <el-form-item label="无单良品数量" prop="noPlanLpsl">
          <el-input
            v-model="form.noPlanLpsl"
            placeholder="请输入无单良品数量"
          />
        </el-form-item>
        <el-form-item label="模具编号" prop="noPlanMjbh">
          <el-input v-model="form.noPlanMjbh" placeholder="请输入模具编号" />
        </el-form-item>
        <el-form-item label="分配时间" prop="noPlanTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.noPlanTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择分配时间"
          ></el-date-picker>
        </el-form-item>
        <el-form-item label="未分配前良品数量" prop="noPlanBeforeLpsl">
          <el-input
            v-model="form.noPlanBeforeLpsl"
            placeholder="请输入未分配前良品数量"
          />
        </el-form-item>
        <el-form-item label="分配后良品数量" prop="noPlanAfterLpsl">
          <el-input
            v-model="form.noPlanAfterLpsl"
            placeholder="请输入分配后良品数量"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  addNoPlanAssign,
  delNoPlanAssign,
  getNoPlanAssign,
  listNoPlanAssign,
  updateNoPlanAssign,
} from "@/api/production/NoPlanAssign";
import { exportTable } from "@/utils/ruoyi";

export default {
  name: "NoPlanAssign",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      openSearch: false,
      // 总条数
      total: 0,
      // NoPlanAssign表格数据
      NoPlanAssignList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 分配时间时间范围
      daterangeNoPlanTime: [
        this.$moment().startOf("month").toDate(),
        this.$moment().add(100, "year").endOf("month").toDate(),
      ],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 25,
        noPlanJtbh: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询无单分配列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeNoPlanTime && "" != this.daterangeNoPlanTime) {
        this.queryParams.params["beginNoPlanTime"] = this.moment(
          this.daterangeNoPlanTime[0]
        ).format("YYYY-MM-DD HH:mm:ss");
        this.queryParams.params["endNoPlanTime"] = this.moment(
          this.daterangeNoPlanTime[1]
        ).format("YYYY-MM-DD HH:mm:ss");
      }
      this.queryParams.pageNum = 1;
      listNoPlanAssign(this.queryParams).then((response) => {
        this.NoPlanAssignList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //load
    load() {
      if (
        this.queryParams.pageNum <
        Math.ceil(this.total / this.queryParams.pageSize)
      ) {
        this.queryParams.pageSize = 25;
        this.queryParams.pageNum++;
        listNoPlanAssign(this.queryParams).then((response) => {
          this.NoPlanAssignList = this.NoPlanAssignList.concat(response.rows);
          this.total = response.total;
          this.loading = false;
        });
      }
      return 0;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openSearch = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        noPlanId: null,
        noPlanZzdh: null,
        noPlanSodh: null,
        noPlanWldm: null,
        noPlanScsl: null,
        noPlanJtbh: null,
        noPlanHmsl: null,
        noPlanLpsl: null,
        noPlanMjbh: null,
        noPlanTime: null,
        noPlanBeforeLpsl: null,
        noPlanAfterLpsl: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.NoPlanAssignList = [];
      this.getList();
      this.openSearch = false;
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeNoPlanTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.noPlanId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加无单分配记录";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const noPlanId = row.noPlanId || this.ids;
      getNoPlanAssign(noPlanId).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改无单分配记录";
      });
    },
    handleSearch() {
      this.reset();
      this.title = "查询无单分配记录";
      this.openSearch = true;
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.noPlanId != null) {
            updateNoPlanAssign(this.form).then((response) => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addNoPlanAssign(this.form).then((response) => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const noPlanIds = row.noPlanId || this.ids;
      this.$confirm(
        '是否确认删除NoPlanAssign编号为"' + noPlanIds + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(function () {
          return delNoPlanAssign(noPlanIds);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport(a, b) {
      exportTable(a, b);
    },
  },
};
</script>

<style lang="scss" scoped></style>
